﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplicationGDI4._2
{    
    public enum DrawMode { Pen, Line };

    public partial class Form1 : Form
    {
        private bool Drawing = false;
        public Form1()
        {
            InitializeComponent();
            button1.BackColor = Color.Black;
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            //if (mode == DrawMode.Line)
            //{
            //    if (line.IsEmpty)
            //    {
            //        line = new Point(e.X, e.Y);
            //    }
            //    else
            //    {
            //        Graphics gr = CreateGraphics();
            //        gr.DrawLine(new Pen(button1.BackColor, trackBar1.Value), line, new Point(e.X, e.Y));
            //        line = new Point();
            //    }
            //}
            Drawing = true;
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (mode == DrawMode.Pen)
            {
                Graphics g = Graphics.FromImage(b);
                g.CopyFromScreen(Form1.ActiveForm.Left + 7, Form1.ActiveForm.Top + 25, -1, -1, this.Size);
                Invalidate();
                this.BackgroundImage = b;
                g.Dispose();
            }

            if (mode == DrawMode.Line)
            {
                Invalidate();
                this.BackgroundImage = b;
            }

            Drawing = false;
            tmp = new Point();
        }
        
        Point tmp;
        //Point line;
        DrawMode mode = DrawMode.Pen;
        Bitmap bit = new Bitmap(825, 369);
        Bitmap bit1 = new Bitmap(813, 336);
        Bitmap b = new Bitmap(825, 369);

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (Drawing == true)
            {
                switch (mode)
                {
                    case DrawMode.Pen:
                        if (tmp.IsEmpty)
                        {
                            tmp = new Point(e.X, e.Y);
                        }

                        //Point p = new Point(e.X, e.Y);

                        Graphics gr = CreateGraphics();
                        gr.DrawLine(new Pen(button1.BackColor, trackBar1.Value), tmp, new Point(e.X, e.Y));                            
                        tmp.X = e.X;
                        tmp.Y = e.Y;

                        break;
                    case DrawMode.Line:
                        if (tmp.IsEmpty)
                        {
                            tmp = new Point(e.X, e.Y);
                        }

                        Invalidate();
                        Graphics gr1 = CreateGraphics();
                        gr1.DrawLine(new Pen(button1.BackColor, trackBar1.Value), tmp, new Point(e.X, e.Y));
                        //Graphics grr = Graphics.FromImage(bit);
                        //grr.CopyFromScreen(Form1.ActiveForm.Left+7, Form1.ActiveForm.Top+25, -1, -1, this.Size);

                        break;
                }
            }
            else
            {
                return;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();

            if (cd.ShowDialog() == DialogResult.OK)
            {
                (sender as Button).BackColor = cd.Color;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            mode = DrawMode.Line;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Invalidate();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            mode = DrawMode.Pen;
        }
    }
}

/*
 * прямоугольник и элипс
 * ломанная линия (curve)
*/